//
//  SimpleSpinningCube.cpp
//  NanoOSX
//
//  Created by Jeppe Nielsen on 9/8/13.
//  Copyright (c) 2013 Jeppe Nielsen. All rights reserved.
//

#include "Engine.hpp"
#include "OpenGL.hpp"
#include "GameWorld.hpp"
#include "RenderSystem.hpp"
#include "Light.hpp"
#include "FirstPersonMoverSystem.hpp"
#include "PhysicsSystem.hpp"

using namespace Nano;

class SimpleSpinningCube : public GameState<SimpleSpinningCube> {
  
    GameWorld world;
    RenderSystem* renderer;
    GameObject* cube;
    float rotation;
    
    void Initialize() {
        renderer = world.CreateSystem<RenderSystem>();
        world.CreateSystem<FirstPersonMoverSystem>()->Input = &Input;
                    
        GameObject* camera = world.CreateObject();
        camera->AddComponent<Transform>()->Position = Vector3(0,0,4);
        camera->AddComponent<Camera>()->Viewport = Manager().Viewport();
        camera->AddComponent<FirstPersonMover>();
        
        GameObject* light = world.CreateObject();
        light->AddComponent<Transform>()->Rotation = Quaternion::LookAt(Vector3(1,1,1), 0, Vector3(0,1,0));
        light->AddComponent<Light>();
        
        GameObject* ambientLight = world.CreateObject();
        ambientLight->AddComponent<Light>()->Type = Light::LightType::Point;
        ambientLight->AddComponent<Colorable>()->Color = Colour::White() * 0.25f;
                
        cube = world.CreateObject();
        cube->AddComponent<Transform>();
        cube->AddComponent<Mesh>()->AddCube(0, 1);
        cube->AddComponent<TextureComponent>()->Texture().LoadFromPng("grass.png");
        cube->AddComponent<Material>()->LightingEnabled = true;
        
        rotation = 0;
    }
    
    void Update(float dt) {
        rotation += dt;
        world.Update(dt);
        cube->GetComponent<Transform>()->Rotation = Quaternion(rotation, Vector3(1,1,0).Normalized());
    }
    
    void Render() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_CULL_FACE);
        glEnable(GL_DEPTH);
        renderer->Render();
    }
};

/*
int main() {
    Engine e;
    e.Start<SimpleSpinningCube>();
}
*/

